package com.fbt.andon.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fbt.andon.domain.Days;
import com.fbt.andon.domain.Facility;
import com.fbt.andon.domain.StopLine;
import com.fbt.andon.domain.Worker;
import com.fbt.andon.service.impl.FacilityServiceImpl;

import com.linkwechat.common.annotation.Log;
import com.linkwechat.common.core.controller.BaseController;
import com.linkwechat.common.core.domain.AjaxResult;
import com.linkwechat.common.enums.BusinessType;
import com.linkwechat.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 机器设备Controller
 *
 * @author fengtao
 * @date 2021-08-13
 */
@RestController
@RequestMapping("/facility")
@Api(description = "机器设备api")
public class FacilityController extends BaseController {
    @Autowired
    private FacilityServiceImpl facilityService;

    //@PreAuthorize(hasPermi = "mes:andon:query")
    @GetMapping("/query")
    @ApiOperation("查询所有机器设备")
    public AjaxResult findIssueList() {
        startPage();
        return AjaxResult.success("查询成功", facilityService.list(null));
    }


    //@PreAuthorize(hasPermi = "mes:andon:query")
    @PostMapping("/listByName/{name}")
    @ApiOperation("根据设备名模糊查询产线信息")
    public AjaxResult list(@PathVariable("name") String name) {
        startPage();
        QueryWrapper<Facility> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        return AjaxResult.success("查询成功", facilityService.list(wrapper));
    }


    //@PreAuthorize(hasPermi = "mes:andon:query")
    @PostMapping("/listByState/{state}")
    @ApiOperation("根据状态模糊查询产线信息")
    public AjaxResult listByState(@PathVariable("state") String state) {
        startPage();
        QueryWrapper<Facility> wrapper = new QueryWrapper<>();
        wrapper.like("state", state);
        return AjaxResult.success("查询成功", facilityService.list(wrapper));
    }


    //@PreAuthorize(hasPermi = "mes:andon:query")
    @ApiOperation("获取机器设备详细信息，参数pkId")
    @GetMapping(value = "/getDaysById/{pkId}")
    public AjaxResult getInfo(@PathVariable("pkId") Integer pkId) {
        return AjaxResult.success("获取成功", facilityService.getById(pkId));
    }


    //@PreAuthorize(hasPermi = "mes:andon:add")
    @ApiOperation("新增机器设备，参数facility")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Facility facility) {
        return AjaxResult.success("新增成功", facilityService.save(facility));
    }

    //@PreAuthorize(hasPermi = "mes:andon:add")
    @ApiOperation("修改机器设备，参数facility")
    @PostMapping("/updateById")
    public AjaxResult edit(@RequestBody Facility facility) {
        return AjaxResult.success("修改成功", facilityService.updateById(facility));
    }


    //@PreAuthorize(hasPermi = "mes:andon:remove")
    @ApiOperation("删除机器设备，参数pkIds")
    @PostMapping("/batchRemove/{pkIds}")
    public AjaxResult removes(@PathVariable("pkIds") Integer[] ids) {
        return AjaxResult.success("删除成功", facilityService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 导出产线列表
     */
    //@PreAuthorize(hasPermi = "mes:andon:export")
    @Log(title = "产线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Facility facility) throws IOException {
        List<Facility> list = facilityService.list(null);
        ExcelUtil<Facility> util = new ExcelUtil<Facility>(Facility.class);
        //util.exportExcel(response, list, "line");
    }
}
